package com.wyzz.hr.action;

import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.util.*;
import org.hibernate.criterion.DetachedCriteria;

import com.wyzz.hr.base.DWRUtil;
import com.wyzz.hr.bo.IEmpBenefitPlanBo;
import com.wyzz.hr.bo.IEmpSalaryAcctitemsBo;
import com.wyzz.hr.bo.IEmpsalarydatadefBo;
import com.wyzz.hr.bo.IEmpsalaryperiodBo;
import com.wyzz.hr.pojo.Empbenefitplan;
import com.wyzz.hr.pojo.Employee;
import com.wyzz.hr.pojo.Empsalaryacctitems;
import com.wyzz.hr.pojo.Empsalaryacctversion;
import com.wyzz.hr.pojo.Empsalarydatadef;
import com.wyzz.hr.pojo.Empsalaryperiod;
import com.wyzz.hr.pojo.Location;
import com.wyzz.hr.spring.SpringBeanFactory;
import com.wyzz.hr.util.BaseCrit;
import com.wyzz.hr.util.DateUtil;
import com.wyzz.hr.util.Pager;
import com.wyzz.hr.util.output.FieldOperate;

public class SearchBeneHistoryAction extends CompAction {

	public SearchBeneHistoryAction() {
		emp = new Employee();
		page = new Pager();
		empType = null;
		locationList = null;
		startYear = DateUtil.formatTodayToS("yyyy");
		startMonth = DateUtil.formatTodayToS("MM");
		endYear = DateUtil.formatTodayToS("yyyy");
		endMonth = DateUtil.formatTodayToS("MM");
		yearMonthCate = "0";
		outputIoName = "OEmpBenefitHistory";
	}

	public String execute() throws Exception {
		getDrillDownList();
		ebpList = searchEbp();
		IEmpBenefitPlanBo ebpBo = (IEmpBenefitPlanBo) getBean("empbenefitplanBo");
		ebpBo.calcEbp(ebpList);
		if ("export".equals(searchOrExport)) {
			if ("yearMonth".equals(exportType)
					|| "belongYearMonth".equals(exportType))
				ebpList = ebpBo.sumEbpList(ebpList, exportType);
			IEmpsalarydatadefBo empsalarydatadefBo = (IEmpsalarydatadefBo) SpringBeanFactory
					.getBean("empsalarydatadefBo");
			List dataDefList = empsalarydatadefBo.getBenedatadefs();
			List fieldAddList = generateAddTitleList(dataDefList);
			ebpBo.processDataForExport(ebpList, dataDefList);
			return downloadToExcel(ebpList, outputIoName, outmatchModelId,
					"ebpEmpno", fieldAddList);
		} else {
			return "success";
		}
	}

	private void getDrillDownList() {
		locationList = getDrillDown("Location", new String[0]);
		empType = getDrillDown("EmpType", new String[0]);
		ebfTypeList = getDrillDown("BenefitType", new String[0]);
		empStatus = getDrillDown("EmpStatus", new String[0]);
		years = getDrillDown("ebpYear", new String[0]);
	}

	private List searchEbp() {
		DetachedCriteria dc = searchBenefitPlan_DC();
		addCriteria(dc);
		IEmpBenefitPlanBo ebpBo = (IEmpBenefitPlanBo) getBean("empbenefitplanBo");
		return ebpBo.findEbp(dc, page, searchOrExport);
	}

	private DetachedCriteria searchBenefitPlan_DC() {
		DetachedCriteria dc = DetachedCriteria.forClass(Empbenefitplan.class);
		dc.createAlias(Empbenefitplan.PROP_EBP_EMP_NO, "emp", 1);
		dc.createAlias((new StringBuilder()).append("emp.").append(
				Employee.PROP_EMP_DEPT_NO).toString(), "dept", 1);
		dc.createAlias((new StringBuilder()).append("emp.").append(
				Employee.PROP_EMP_LOCATION_NO).toString(), "location", 1);
		dc.createAlias((new StringBuilder()).append("emp.").append(
				Employee.PROP_EMP_TYPE).toString(), "empType", 1);
		dc.createAlias((new StringBuilder()).append("emp.").append(
				Employee.PROP_EMP_BENEFIT_TYPE).toString(), "beneType", 1);
		dc.createAlias((new StringBuilder()).append("emp.").append(
				Employee.PROP_EMP_PB_NO).toString(), "empPbNo", 1);
		dc.createAlias(Empbenefitplan.PROP_EBP_EBP_ESAV_ID, "ebpEsavId", 1);
		dc.createAlias((new StringBuilder()).append("ebpEsavId.").append(
				Empsalaryacctversion.PROP_ESAV_ESAC).toString(), "esac", 1);
		return dc;
	}

	private void addCriteria(DetachedCriteria dc) {
		if ("SUB".equals(super.authorityCondition))
			addSubDC(dc, "emp", 2);
		BaseCrit.addEmpDC(dc, "emp", emp.getEmpName());
		BaseCrit.addDeptDC(dc, (new StringBuilder()).append("emp.").append(
				Employee.PROP_EMP_DEPT_NO).toString(), (new StringBuilder())
				.append("emp.").append(Employee.PROP_EMP_PB_NO).toString(),
				null, emp.getEmpDeptNo());
		BaseCrit.addDC(dc, (new StringBuilder()).append("emp.").append(
				Employee.PROP_EMP_LOCATION_NO).toString(), Location.PROP_ID,
				new Object[] { emp.getEmpLocationNo() });
		BaseCrit.addDC(dc, (new StringBuilder()).append("emp.").append(
				Employee.PROP_EMP_BENEFIT_TYPE).toString(), "id",
				new Object[] { emp.getEmpBenefitType() });
		BaseCrit.addDC(dc, Empbenefitplan.PROP_EBP_STATUS, "eq",
				new Integer[] { beneCategory });
		if ("0".equals(yearMonthCate)) {
			BaseCrit.addDC(dc, Empbenefitplan.PROP_EBP_YEARMONTH, "ge",
					new String[] { (new StringBuilder()).append(startYear)
							.append(startMonth).toString() });
			BaseCrit.addDC(dc, Empbenefitplan.PROP_EBP_YEARMONTH, "le",
					new String[] { (new StringBuilder()).append(endYear)
							.append(endMonth).toString() });
		} else if ("1".equals(yearMonthCate)) {
			BaseCrit.addDC(dc, Empbenefitplan.PROP_EBP_BELONG_YEARMONTH, "ge",
					new String[] { (new StringBuilder()).append(startYear)
							.append(startMonth).toString() });
			BaseCrit.addDC(dc, Empbenefitplan.PROP_EBP_BELONG_YEARMONTH, "le",
					new String[] { (new StringBuilder()).append(endYear)
							.append(endMonth).toString() });
		}
		addOrders(dc, page,
				new String[] {
						(new StringBuilder()).append(
								Empbenefitplan.PROP_EBP_YEARMONTH).append(
								"-down").toString(),
						(new StringBuilder()).append(
								Empbenefitplan.PROP_EBP_BELONG_YEARMONTH)
								.append("-down").toString(),
						(new StringBuilder()).append("emp.").append(
								Employee.PROP_EMP_DISTINCT_NO).append("-up")
								.toString() });
	}

	private List generateAddTitleList(List dataDefList) {
		List addTitleList = new ArrayList();
		Empsalarydatadef datadef = null;
		for (int i = 0; i < dataDefList.size(); i++) {
			datadef = (Empsalarydatadef) dataDefList.get(i);
			FieldOperate operate = new FieldOperate();
			operate.setColWidth(15);
			operate.setDataType("decimal");
			operate.setFieldName((new StringBuilder()).append("outPutList.")
					.append(datadef.getEsddId()).toString());
			operate.setShowName(datadef.getEsddName());
			addTitleList.add(operate);
		}

		FieldOperate operate = new FieldOperate();
		operate.setColWidth(30);
		operate.setDataType("string");
		operate.setFieldName("ebpComments");
		operate.setShowName("\u5907\u6CE8");
		addTitleList.add(operate);
		return addTitleList;
	}

	public List getEbpItemsById(String ebpId) {
		String flt = DWRUtil.checkAuth("getEbpById", "getEbpById");
		if ("error".equalsIgnoreCase(flt))
			return null;
		IEmpBenefitPlanBo ebpBo = (IEmpBenefitPlanBo) getBean("empbenefitplanBo");
		List ebpList = ebpBo.searchEbpById(new String[] { ebpId });
		Empbenefitplan ebp = (Empbenefitplan) ebpList.get(0);
		ebp.decryEMPPlan(ebp);
		IEmpSalaryAcctitemsBo itemsBo = (IEmpSalaryAcctitemsBo) SpringBeanFactory
				.getBean("empsalaryacctitemsBo");
		List acctItems = itemsBo.getItemsByAcctversion(ebp.getEbpEsavId()
				.getId());
		Class ownerClass = ebp.getClass();
		Method ebpMethod = null;
		BigDecimal ebpBD = null;
		Empsalaryacctitems item;
		for (Iterator i$ = acctItems.iterator(); i$.hasNext(); item
				.setItemValue(ebpBD)) {
			item = (Empsalaryacctitems) i$.next();
			try {
				ebpMethod = ownerClass.getMethod((new StringBuilder()).append(
						"getEbpColumn").append(item.getEsaiDataSeq())
						.toString(), new Class[0]);
				ebpBD = (BigDecimal) ebpMethod.invoke(ebp, new Object[0]);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		return acctItems;
	}

	public String getEspStatus(String yearMonth) {
		yearMonth = yearMonth.replaceAll("-", "");
		IEmpsalaryperiodBo periodBo = (IEmpsalaryperiodBo) getBean("empsalaryperiodBO");
		Empsalaryperiod period = periodBo.loadEspdStatus(yearMonth);
		if (period == null || period.getEspdStatus().intValue() == 0)
			return "true";
		else
			return "false";
	}

	public List getEmpType() {
		return empType;
	}

	public void setEmpType(List empType) {
		this.empType = empType;
	}

	public List getLocationList() {
		return locationList;
	}

	public void setLocationList(List locationList) {
		this.locationList = locationList;
	}

	public List getEbfTypeList() {
		return ebfTypeList;
	}

	public void setEbfTypeList(List ebfTypeList) {
		this.ebfTypeList = ebfTypeList;
	}

	public Employee getEmp() {
		return emp;
	}

	public void setEmp(Employee emp) {
		this.emp = emp;
	}

	public String getEmpCondition() {
		return empCondition;
	}

	public void setEmpCondition(String empCondition) {
		this.empCondition = empCondition;
	}

	public Pager getPage() {
		return page;
	}

	public void setPage(Pager page) {
		this.page = page;
	}

	public List getEbpList() {
		return ebpList;
	}

	public void setEbpList(List ebpList) {
		this.ebpList = ebpList;
	}

	public String getOrderFieldName() {
		return orderFieldName;
	}

	public void setOrderFieldName(String orderFieldName) {
		this.orderFieldName = orderFieldName;
	}

	public String getCanEdit() {
		return canEdit;
	}

	public void setCanEdit(String canEdit) {
		this.canEdit = canEdit;
	}

	public Integer getBeneCategory() {
		return beneCategory;
	}

	public void setBeneCategory(Integer beneCategory) {
		this.beneCategory = beneCategory;
	}

	public String getEndMonth() {
		return endMonth;
	}

	public void setEndMonth(String endMonth) {
		this.endMonth = endMonth;
	}

	public String getEndYear() {
		return endYear;
	}

	public void setEndYear(String endYear) {
		this.endYear = endYear;
	}

	public String getStartMonth() {
		return startMonth;
	}

	public void setStartMonth(String startMonth) {
		this.startMonth = startMonth;
	}

	public String getStartYear() {
		return startYear;
	}

	public void setStartYear(String startYear) {
		this.startYear = startYear;
	}

	public List getYears() {
		return years;
	}

	public void setYears(List years) {
		this.years = years;
	}

	public String getOutmatchModelId() {
		return outmatchModelId;
	}

	public void setOutmatchModelId(String outmatchModelId) {
		this.outmatchModelId = outmatchModelId;
	}

	public String getSearchOrExport() {
		return searchOrExport;
	}

	public void setSearchOrExport(String searchOrExport) {
		this.searchOrExport = searchOrExport;
	}

	public String getOutputIoName() {
		return outputIoName;
	}

	public String getExportType() {
		return exportType;
	}

	public void setExportType(String exportType) {
		this.exportType = exportType;
	}

	public String getYearMonthCate() {
		return yearMonthCate;
	}

	public void setYearMonthCate(String yearMonthCate) {
		this.yearMonthCate = yearMonthCate;
	}

	private static final long serialVersionUID = 1L;
	private String empCondition;
	private Employee emp;
	private Pager page;
	private String orderFieldName;
	private List empType;
	private List locationList;
	private List ebfTypeList;
	private List ebpList;
	private String canEdit;
	private Integer beneCategory;
	private List years;
	private String startYear;
	private String startMonth;
	private String endYear;
	private String endMonth;
	private String yearMonthCate;
	private String exportType;
	private String searchOrExport;
	private String outmatchModelId;
	private String outputIoName;
}
